package com.arvato.common.task;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.arvato.common.CSVUtils;
import com.arvato.common.solr.SolrUtil;
import com.arvato.common.util.JsonUtil;
import com.arvato.wxwork.bean.bean_do.member.WxworkSysCcPointDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkSysCustbasicDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkSysLoyaltyDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkSysOrderDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkSysOrderItemDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkSysRedempresultheaderDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkSysRedempresultheaderItemDo;
import com.arvato.wxwork.bean.bean_do.staff.WxworkSysBaDo;
import com.arvato.wxwork.bean.bean_do.staff.WxworkSysCounterDo;
import com.arvato.wxwork.service.member.WxworkSySBaService;
import com.arvato.wxwork.service.member.WxworkSySCCpointService;
import com.arvato.wxwork.service.member.WxworkSySCounterService;
import com.arvato.wxwork.service.member.WxworkSySCustService;
import com.arvato.wxwork.service.member.WxworkSySLoyaltyService;
import com.arvato.wxwork.service.member.WxworkSySOrderItemService;
import com.arvato.wxwork.service.member.WxworkSySOrderService;
import com.arvato.wxwork.service.member.WxworkSySRedempResultHeaderService;

@Component
public class WxWorkImportTask {

	@Autowired
	private WxworkSySOrderItemService wxworkSySOrderItemService;

	private Logger logger = LoggerFactory.getLogger(WxWorkImportTask.class);

	@Autowired
	private SolrUtil solrUtil;

	@Autowired
	private WxworkSySCounterService wxworkSySCounterService;
	@Autowired
	private WxworkSySBaService wxworkSysBaService;

	@Autowired
	private WxworkSySOrderService wxworkSySOrderService;

	@Autowired
	private WxworkSySCustService wworkSySCustService;
	@Autowired
	private WxworkSySLoyaltyService wxworkSySLoyaltyService;

	@Autowired
	private WxworkSySCCpointService wxworkSySCCpointService;
	
	@Autowired
	private WxworkSySRedempResultHeaderService wxworkSySRedempResultHeaderService;
	

	public void importData() {
		importBa();
		importCounter();
		importCustBasic();
		importOrder();
		importOrderItem();
		importLoyalty();
		importRedempResultItem();
		importRedempResultHeader();
		importCCPoint();
	}
   /**
    * 兑礼
    */
	public void importRedempResultHeader() {
		try {
			List<String> str = CSVUtils.convert(
					new File("D:\\zipfile\\Seibel - Miracle\\RedempResultHeader-Siebel-La Roche Posay-20160623.csv"));
			List<WxworkSysRedempresultheaderDo> list = new ArrayList<WxworkSysRedempresultheaderDo>();
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysRedempresultheaderDo ba = new WxworkSysRedempresultheaderDo();
				ba.setCountry(arg[0].replaceAll("\"", ""));
				ba.setOrganization(arg[1].replaceAll("\"", ""));
				ba.setIdentSource(arg[2].replaceAll("\"", ""));
				ba.setPersonId(arg[3].replaceAll("\"", ""));
				ba.setOrderNum(arg[4].replaceAll("\"", ""));
				ba.setOrderDt(arg[5].replaceAll("\"", ""));
				ba.setCounterId(arg[6].replaceAll("\"", ""));
				ba.setBaId(arg[7].replaceAll("\"", ""));
				ba.setPointType(arg[8].replaceAll("\"", ""));
				ba.setType(arg[9].replaceAll("\"", ""));
				ba.setAmount(arg[10].replaceAll("\"", ""));
				ba.setRemark(arg[11].replaceAll("\"", ""));
				ba.setStatus(arg[12].replaceAll("\"", ""));
				ba.setQuantity(arg[13].replaceAll("\"", ""));
				ba.setGpbuyFlag(arg[14].replaceAll("\"", ""));
				ba.setPoints(arg[15].replaceAll("\"", ""));
				ba.setSource(arg[16].replaceAll("\"", ""));
				ba.setRedempbatch(arg[17].replaceAll("\"", ""));
				ba.setDeliverMethod(arg[18].replaceAll("\"", ""));
				ba.setEmail(arg[19].replaceAll("\"", ""));
				ba.setConsignee(arg[20].replaceAll("\"", ""));
				ba.setConsigneePhone(arg[21].replaceAll("\"", ""));
				ba.setConsigneeProvince(arg[22].replaceAll("\"", ""));
				ba.setConsigneeCity(arg[23].replaceAll("\"", ""));
				ba.setConsigneeAddr(arg[24].replaceAll("\"", ""));
				ba.setPaymentType(arg[25].replaceAll("\"", ""));
				ba.setOrderTime(arg[26].replaceAll("\"", ""));
				ba.setOrderName(arg[27].replaceAll("\"", ""));
				ba.setMemberLevel(arg[28].replaceAll("\"", ""));
				list.add(ba);
			}
			wxworkSySRedempResultHeaderService.insertBatchRedempResultHeader(list);
			//solrUtil.insert(mapList)
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

	public void importLoyalty() {
		try {
			List<String> str = CSVUtils
					.convert(new File("D:\\zipfile\\Seibel - Miracle\\Loyalty-Siebel-La Roche Posay-20160623.csv"));
			List<WxworkSysLoyaltyDo> list = new ArrayList<WxworkSysLoyaltyDo>();
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysLoyaltyDo ba = new WxworkSysLoyaltyDo();
				ba.setCountry(arg[0].replaceAll("\"", ""));
				ba.setOrganization(arg[1].replaceAll("\"", ""));
				ba.setIdentSource(arg[2].replaceAll("\"", ""));
				ba.setPersonId(arg[3].replaceAll("\"", ""));
				ba.setLoycardPointBalance(arg[4].replaceAll("\"", ""));
				ba.setLoycardPointExpDate(arg[5].replaceAll("\"", ""));
				ba.setLoycardNewpoint(arg[6].replaceAll("\"", ""));
				ba.setLoycardNewpointExpDate(arg[7].replaceAll("\"", ""));
				ba.setMemTypeCd(arg[8].replaceAll("\"", ""));
				ba.setLoyLevel(arg[9].replaceAll("\"", ""));
				ba.setLoyLevelLastUpdatedDt(arg[10].replaceAll("\"", ""));
				ba.setLoycardStartdt(arg[11].replaceAll("\"", ""));
				ba.setLoycardEnddt(arg[12].replaceAll("\"", ""));
				ba.setLoyExpLevel(arg[13].replaceAll("\"", ""));
				ba.setBlackDaFlg(arg[4].replaceAll("\"", ""));
				ba.setFrozenFlg(arg[15].replaceAll("\"", ""));
				ba.setPureControlFlg(arg[16].replaceAll("\"", ""));
				ba.setActivetierStartdt(arg[17].replaceAll("\"", ""));
				ba.setActivetierEnddt(arg[18].replaceAll("\"", ""));
				ba.setMemStatus(arg[19].replaceAll("\"", ""));
				ba.setMemJoinedDate(arg[20].replaceAll("\"", ""));
				list.add(ba);
			}
			wxworkSySLoyaltyService.insertBatchLoyaltyDo(list);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
	 /**
	    * 兑礼明细
	    */
	public void importRedempResultItem() {
		try {
			List<String> str = CSVUtils.convert(
					new File("D:\\zipfile\\Seibel - Miracle\\RedempResultItem-Siebel-La Roche Posay-20160623.csv"));
			List<WxworkSysRedempresultheaderItemDo> list = new ArrayList<WxworkSysRedempresultheaderItemDo>();
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysRedempresultheaderItemDo ba = new WxworkSysRedempresultheaderItemDo();
				ba.setCountry(arg[0].replaceAll("\"", ""));
				ba.setOrganization(arg[1].replaceAll("\"", ""));
				ba.setIdentSource(arg[2].replaceAll("\"", ""));
				ba.setPersonId(arg[3].replaceAll("\"", ""));
				ba.setOrderNum(arg[4].replaceAll("\"", ""));
				ba.setBarCode(arg[5].replaceAll("\"", ""));
				ba.setProduct(arg[6].replaceAll("\"", ""));
				ba.setType(arg[7].replaceAll("\"", ""));
				ba.setQuantity(arg[8].replaceAll("\"", ""));
				ba.setReturnReasonCd(arg[9].replaceAll("\"", ""));
				ba.setPrice(arg[10].replaceAll("\"", ""));
				ba.setProductOrigin(arg[11].replaceAll("\"", ""));
				ba.setProductType(arg[12].replaceAll("\"", ""));
				ba.setCouponCode(arg[13].replaceAll("\"", ""));
				list.add(ba);
			}
		//	wxworkSySRedempResultHeaderService.insertBatchRedempResultHeaderItem(list);
				wxworkSySRedempResultHeaderService.insertBatchRedempResultHeaderItem(list);

		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

	/**
	 * 积分
	 */
	public void importCCPoint() {
		try {
			List<String> str = CSVUtils.convert(
					new File("D:\\zipfile\\Seibel - Miracle\\siebel-CC-Point-Item-La Roche Posay-20160623.csv"));
			List<WxworkSysCcPointDo> list = new ArrayList<WxworkSysCcPointDo>();
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysCcPointDo ba = new WxworkSysCcPointDo();
				ba.setCountry(arg[0].replaceAll("\"", ""));
				ba.setOrganization(arg[1].replaceAll("\"", ""));
				ba.setIdentSource(arg[2].replaceAll("\"", ""));
				ba.setPersonId(arg[3].replaceAll("\"", ""));
				ba.setOrderNum(arg[4].replaceAll("\"", ""));
				ba.setPointType(arg[5].replaceAll("\"", ""));
				ba.setAccrualedValue(arg[6].replaceAll("\"", ""));
				ba.setQualifyingFlag(arg[7].replaceAll("\"", ""));
				ba.setExpirationDate(arg[8].replaceAll("\"", ""));
				ba.setUsedValue(arg[9].replaceAll("\"", ""));
				ba.setPromotion(arg[10].replaceAll("\"", ""));
				ba.setRule(arg[11].replaceAll("\"", ""));
				ba.setAction(arg[12].replaceAll("\"", ""));
				ba.setComments(arg[12].replaceAll("\"", ""));
				ba.setRowId(arg[13].replaceAll("\"", ""));
				ba.setCreatedDate(arg[14].replaceAll("\"", ""));
				ba.setTxnComments(arg[15].replaceAll("\"", ""));
				list.add(ba);
			}
			wxworkSySCCpointService.insertBatchCCpoint(list);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}

	}

	/**
	 * 订单明细
	 */
	public void importOrderItem() {
		try {
			List<String> str = CSVUtils
					.convert(new File("D:\\zipfile\\Seibel - Miracle\\Orderitem-Siebel-La Roche Posay-20160623.csv"));
			List<WxworkSysOrderItemDo> list = new ArrayList<WxworkSysOrderItemDo>();
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysOrderItemDo ba = new WxworkSysOrderItemDo();
				ba.setCountry(arg[0].replaceAll("\"", ""));
				ba.setOrganization(arg[1].replaceAll("\"", ""));
				ba.setIdentSource(arg[2].replaceAll("\"", ""));
				ba.setPersonId(arg[3].replaceAll("\"", ""));
				ba.setOrderNum(arg[4].replaceAll("\"", ""));
				ba.setItemNum(arg[5].replaceAll("\"", ""));
				ba.setProduct(arg[6].replaceAll("\"", ""));
				ba.setType(arg[7].replaceAll("\"", ""));
				ba.setQantity(arg[8].replaceAll("\"", ""));
				ba.setReturnReasonCd(arg[9].replaceAll("\"", ""));
				ba.setPrice(arg[10].replaceAll("\"", ""));
				ba.setProductType(arg[11].replaceAll("\"", ""));
				ba.setCouponCode(arg[12].replaceAll("\"", ""));
				list.add(ba);
			}
			wxworkSySOrderItemService.insertBatchOrderItem(list);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}

	}

	public void importCustBasic() {
		try {
			List<String> str = CSVUtils
					.convert(new File("D:\\zipfile\\Seibel - Miracle\\Custbasic-Siebel-La Roche Posay-20160623.csv"));
			List<WxworkSysCustbasicDo> list = new ArrayList<WxworkSysCustbasicDo>();
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysCustbasicDo ba = new WxworkSysCustbasicDo();
				ba.setRecordStatus(arg[0].replaceAll("\"", ""));
				ba.setCountry(arg[1].replaceAll("\"", ""));
				ba.setOrganization(arg[2].replaceAll("\"", ""));
				ba.setIdentSource(arg[3].replaceAll("\"", ""));
				ba.setPersonId(arg[4].replaceAll("\"", ""));
				ba.setOldmembercode(arg[5].replaceAll("\"", ""));
				ba.setCivility(arg[6].replaceAll("\"", ""));
				ba.setLastName(arg[7].replaceAll("\"", ""));
				ba.setFirstName(arg[8].replaceAll("\"", ""));
				ba.setAddr1(arg[9].replaceAll("\"", ""));
				ba.setAddr2(arg[10].replaceAll("\"", ""));
				ba.setZipcode(arg[11].replaceAll("\"", ""));
				ba.setCity(arg[12].replaceAll("\"", ""));
				ba.setProvince(arg[13].replaceAll("\"", ""));
				ba.setRegion(arg[14].replaceAll("\"", ""));
				ba.setAddrCountry(arg[15].replaceAll("\"", ""));
				ba.setOccupation(arg[16].replaceAll("\"", ""));
				ba.setPhone(arg[17].replaceAll("\"", ""));
				ba.setFax(arg[18].replaceAll("\"", ""));
				ba.setMobile(arg[19].replaceAll("\"", ""));
				ba.setOfficephone(arg[20].replaceAll("\"", ""));
				ba.setBirthdayDt(arg[21].replaceAll("\"", ""));
				ba.setBirthYear(arg[22].replaceAll("\"", ""));
				ba.setBirthMonthDay(arg[23].replaceAll("\"", ""));
				ba.setCreationDt(arg[24].replaceAll("\"", ""));
				ba.setSsn(arg[25].replaceAll("\"", ""));
				ba.setMaritalstatus(arg[26].replaceAll("\"", ""));
				ba.setGender(arg[27].replaceAll("\"", ""));
				ba.setEmail(arg[28].replaceAll("\"", ""));
				ba.setIncome(arg[29].replaceAll("\"", ""));
				ba.setCounterId(arg[30].replaceAll("\"", ""));
				ba.setBaId(arg[31].replaceAll("\"", ""));
				ba.setPrefChannel(arg[32].replaceAll("\"", ""));
				ba.setGoneAwayFlg(arg[33].replaceAll("\"", ""));
				ba.setGoneAwayDt(arg[34].replaceAll("\"", ""));
				ba.setAddrPermStatus(arg[35].replaceAll("\"", ""));
				ba.setPhonePermStatus(arg[36].replaceAll("\"", ""));
				ba.setEmailPermStatus(arg[37].replaceAll("\"", ""));
				ba.setSmsPermStatus(arg[38].replaceAll("\"", ""));
				ba.setLoyMemberFlg(arg[39].replaceAll("\"", ""));
				ba.setLoycardNumber(arg[40].replaceAll("\"", ""));
				ba.setLoycardStartdt(arg[41].replaceAll("\"", ""));
				ba.setLoycardStatus(arg[42].replaceAll("\"", ""));
				ba.setPhoneInvFlag(arg[43].replaceAll("\"", ""));
				ba.setMobilePhoneInvFlag(arg[44].replaceAll("\"", ""));
				ba.setEmailInvFlag(arg[45].replaceAll("\"", ""));
			    list.add(ba);
			}
			wworkSySCustService.insertBatchCust(list);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}

	}

	/**
	 * 订单数据
	 */
	public void importOrder() {
		try {
			List<String> str = CSVUtils
					.convert(new File("D:\\zipfile\\Seibel - Miracle\\Order-Siebel-La Roche Posay-20160623.csv"));
			List<WxworkSysOrderDo> list = new ArrayList<WxworkSysOrderDo>();
			
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysOrderDo ba = new WxworkSysOrderDo();
				ba.setCountry(arg[0].replaceAll("\"", ""));
				ba.setOrganization(arg[1].replaceAll("\"", ""));
				ba.setIdentSource(arg[2].replaceAll("\"", ""));
				ba.setPersonId(arg[3].replaceAll("\"", ""));
				ba.setOrderNum(arg[4].replaceAll("\"", ""));
				ba.setOrderDt(arg[5].replaceAll("\"", ""));
				ba.setCounterId(arg[6].replaceAll("\"", ""));
				ba.setBaId(arg[7].replaceAll("\"", ""));
				ba.setType(arg[8].replaceAll("\"", ""));
				ba.setAmount(arg[9].replaceAll("\"", ""));
				ba.setRemark(arg[10].replaceAll("\"", ""));
				ba.setStatus(arg[11].replaceAll("\"", ""));
				ba.setQuantity(arg[12].replaceAll("\"", ""));
				ba.setGpbuyFlag(arg[13].replaceAll("\"", ""));
				ba.setPoints(arg[14].replaceAll("\"", ""));
				ba.setSource(arg[15].replaceAll("\"", ""));
				list.add(ba);
			}
		/*	solrUtil.insert(maplist);
			System.out.println(maplist.size());*/
			wxworkSySOrderService.insertBatchOrder(list);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}

	}

	/**
	 * 导入BA数据
	 */
	public void importBa() {
		try {
			List<String> str = CSVUtils
					.convert(new File("D:\\zipfile\\Seibel - Miracle\\\\BA-Siebel-La Roche Posay-20160623.csv"));
			List<WxworkSysBaDo> list = new ArrayList<WxworkSysBaDo>();
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysBaDo ba = new WxworkSysBaDo();
				ba.setCountry(arg[0].replaceAll("\"", ""));
				ba.setOrganization(arg[1].replaceAll("\"", ""));
				ba.setBaId(arg[2].replaceAll("\"", ""));
				ba.setIdentSource(arg[3].replaceAll("\"", ""));
				ba.setCounterId(arg[4].replaceAll("\"", ""));
				ba.setLastName(arg[5].replaceAll("\"", ""));
				ba.setFirstName(arg[6].replaceAll("\"", ""));
				ba.setPositionType(arg[7].replaceAll("\"", ""));
				ba.setStartDt(arg[8].replaceAll("\"", ""));
				ba.setEndDt(arg[9].replaceAll("\"", ""));
				ba.setSsn(arg[10].replaceAll("\"", ""));
				ba.setPhone(arg[11].replaceAll("\"", ""));
				ba.setMobile(arg[12].replaceAll("\"", ""));
				ba.setEmail(arg[13].replaceAll("\"", ""));
				ba.setAddress(arg[14].replaceAll("\"", ""));
				ba.setAddress2(arg[15].replaceAll("\"", ""));
				ba.setProvince(arg[16].replaceAll("\"", ""));
				ba.setCity(arg[17].replaceAll("\"", ""));
				ba.setZipcode(arg[18].replaceAll("\"", ""));
				list.add(ba);
			}
			wxworkSysBaService.insertBatchBa(list);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

	/**
	 * 导入counter数据
	 */
	public void importCounter() {
		try {
			List<String> str = CSVUtils
					.convert(new File("D:\\zipfile\\Seibel - Miracle\\Counter-Siebel-La Roche Posay-20160623.csv"));
			List<WxworkSysCounterDo> list = new ArrayList<WxworkSysCounterDo>();
			for (int i = 1; i < str.size(); i++) {
				String arg[] = str.get(i).split(";");
				WxworkSysCounterDo ba = new WxworkSysCounterDo();
				ba.setCountry(arg[0].replaceAll("\"", ""));
				ba.setOrganization(arg[1].replaceAll("\"", ""));
				ba.setCounterId(arg[2].replaceAll("\"", ""));
				ba.setIdentSource(arg[3].replaceAll("\"", ""));
				ba.setStatus(arg[4].replaceAll("\"", ""));
				ba.setStartDt(arg[5].replaceAll("\"", ""));
				ba.setEndDt(arg[6].replaceAll("\"", ""));
				ba.setDescEn(arg[7].replaceAll("\"", ""));
				ba.setDescCh(arg[8].replaceAll("\"", ""));
				ba.setAddress(arg[9].replaceAll("\"", ""));
				ba.setProvince(arg[10].replaceAll("\"", ""));
				ba.setCity(arg[11].replaceAll("\"", ""));
				ba.setZipcode(arg[12].replaceAll("\"", ""));
				ba.setPhone(arg[13].replaceAll("\"", ""));
				ba.setGeographicalarea(arg[14].replaceAll("\"", ""));
				ba.setRegion(arg[15].replaceAll("\"", ""));
				ba.setCounternameEn(arg[16].replaceAll("\"", ""));
				ba.setStoregroup(arg[17].replaceAll("\"", ""));
				ba.setStoretype(arg[18].replaceAll("\"", ""));
				ba.setMerchandisingClassification(arg[19].replaceAll("\"", ""));
				ba.setBusinessClassification(arg[20].replaceAll("\"", ""));
				list.add(ba);
			}
			wxworkSySCounterService.insertBatchCounter(list);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

}